<?php
function detail_kelompok_shift($karyawan_id,$periode_id   ){
	my_set_code_js('
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&task=pindahperiode&karyawan_id='.$karyawan_id.'&periode_id=\'+periode_id ;
		}
	');
	$periode_absen_id =get_periode_absen();
	if($periode_id==0){
		$periode_id =get_last_periode_id($periode_absen_id);
	}	
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);	
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$karyawan =  loaddata_karyawan($karyawan_id,false); 
	$kelompok_shift_id =$karyawan['kelompok_shift_id'];

	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10'; 
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.$bulan .'-10'; 
	}
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Gl.'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),   
		'Global Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),   
		'Schedule'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'S. Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),   
		'Status'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
	);
	
	$row= array();
	foreach($tanggals as $tanggal){	
		list($yyyy,$mm,$dd) = explode("-" , $tanggal);	
		$ch = "SELECT * FROM a_realisasi_log 
			WHERE karyawan_id = {$karyawan_id} 
				AND field_tgl='tgl_{$dd}' 
				AND periode_id={$periode_id} 
				ORDER BY realisasi_log_id 
				DESC LIMIT 1";
		$chr = my_query($ch);
		$areals = my_fetch_array($chr);	
		$sc_jadwal_kerja_kelompok_id= isset( $areals['sc_jadwal_kerja_kelompok_id'] ) ? $areals['sc_jadwal_kerja_kelompok_id'] :  0;
		$form_wk=get_jadwal_kerja_kelompok($sc_jadwal_kerja_kelompok_id);
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				'g'=> $areals['gl_status'],
				'stime'=>$areals['gl_time'],
				'wk'=>$form_wk, 
				'time'=>$areals['sc_time'], 
				'status'=>$areals['sc_status'], 
			);
		}else{
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				'g'=> $areals['gl_status'],
				'stime'=>$areals['gl_time'],
				'wk'=>$form_wk, 
				'time'=>$areals['sc_time'], 
				'status'=>$areals['sc_status'], 
			);
		}	
	}
	$datas = table_rows($row);
	$table1 = table_builder($header , $datas ,  2 , false);
	$tanggals = list_kalender($middledata2 ,$enddate  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Gl.'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),   
		'Global Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),   
		'Schedule'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'S. Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),   
		'Status'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
	);
	
	
	$row= array();
	foreach($tanggals as $tanggal){
		list($yyyy,$mm,$dd) = explode("-" , $tanggal);	
		$ch = "SELECT * FROM a_realisasi_log 
			WHERE karyawan_id = {$karyawan_id} 
				AND field_tgl='tgl_{$dd}' 
				AND periode_id={$periode_id} 
				ORDER BY realisasi_log_id 
				DESC LIMIT 1";
		$chr = my_query($ch);
		$areals = my_fetch_array($chr);	
		$sc_jadwal_kerja_kelompok_id= isset( $areals['sc_jadwal_kerja_kelompok_id'] ) ? $areals['sc_jadwal_kerja_kelompok_id'] :  0;
		$form_wk=get_jadwal_kerja_kelompok($sc_jadwal_kerja_kelompok_id);
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				'g'=> $areals['gl_status'],
				'stime'=>$areals['gl_time'],
				'wk'=>$form_wk, 
				'time'=>$areals['sc_time'], 
				'status'=>$areals['sc_status'], 
			);
		}else{
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				'g'=> $areals['gl_status'],
				'stime'=>$areals['gl_time'],
				'wk'=>$form_wk, 
				'time'=>$areals['sc_time'], 
				'status'=>$areals['sc_status'], 
			);
		}	
	}
	$datas = table_rows($row);
	$table2 = table_builder($header , $datas ,  2 , false);	
	$query_check_periode = " SELECT * FROM pr_periode WHERE periode_id< {$periode_absen_id} ORDER BY periode_id DESC";	
	
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('Y-m',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$form_dpwerio = form_dropdown($fperiode_id,$prs);
	$kelompok = my_get_data_by_id('a_kelompok_shift','kelompok_shift_id',$kelompok_shift_id);
	$newline = array(
		'Periode'=>$form_dpwerio ,
		'Tipe Waktu Kerja'=>$karyawan['waktu_kerja'],
		'Kelompok Shift'=>strtoupper( $kelompok['kelompok_shift_kode']),
	);
	  
	$view = karyawan_header_detail($karyawan  ,  true , $newline );
	 
	$split ='<div style="height:470px;"><form method="post" name="multiDataBox">
	<div style="width:49%;float:left">'.$table1.'</div>';
	$split .='<div style="width:49%;float:right">'.$table2.'</div></div>';
	$view .= '<div style="width:98%;text-align:right;">
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=jadwal_wk_karyawan&task=list_detail&karyawan_id='.$karyawan_id.'\'"/>
	</form></div>'   ; 	
	return $view . $split;
}

function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}

function get_jadwal_kerja_kelompok($sc_jadwal_kerja_kelompok_id){
	if($sc_jadwal_kerja_kelompok_id==21){
		return "X" ;
	}	
	$jadwal_kerja_kelompoks = my_get_data_by_id('a_jadwal_kerja_kelompok','jadwal_kerja_kelompok_id',$sc_jadwal_kerja_kelompok_id);
	$waktu_kerja_id=isset( $jadwal_kerja_kelompoks['waktu_kerja_id'] ) ? $jadwal_kerja_kelompoks['waktu_kerja_id'] :  0 ;
	$jadwal_kerjas = my_get_data_by_id('a_waktu_kerja','waktu_kerja_id',$waktu_kerja_id);
	return $jadwal_kerjas['label'].'/'.$jadwal_kerja_kelompoks['kelompok'] ;
}